<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="$emit('closeEdit')"
  />
</template>

<script>
import { updataUserData } from '@/api/user'
import dayjs from 'dayjs'

export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1947, 1, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },

  created () {
    this.currentDate = new Date(this.value)
  },

  methods: {
    async onConfirm (val) {
      const dateTime = dayjs(val).format('YYYY-MM-DD')
      if (dateTime === this.value) return this.$emit('closeEdit')
      this.$toast.loading({
        message: '修改中...',
        forbidClick: true
      })
      try {
        await updataUserData({ birthday: dateTime })
        this.$emit('closeEdit')
        this.$emit('input', dateTime)
        this.$toast('修改成功')
      } catch (err) {
        this.$toast('修改失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
